﻿using Microsoft.Extensions.Configuration;
using MiniFox.Configurations;
using System.Text.RegularExpressions;

namespace MiniFox.Platform.OAuth2.Configurations
{
    /// <summary>
    /// 通行证服务配置
    /// </summary>
    [Section(ELEMENT_NAME)]
    public class PassportConfiguration
    {
        internal const string ELEMENT_NAME = "minifox:oauth2:passport";
        const string AUTH_URI = "/authorization/connect";
        const string SIGNIN_VIA = "signin";
        const int ACTIVATE_EXPIRES = 7200;
        const int PASSPORT_EXPIRES = 7200;
        const int COOKIE_STATE_EXPIRES = 36000;
        Regex _loginNameRegex;
        Regex _emailRegex;
        Regex _cellphoneRegex;
        Regex _passwordRegex;

        /// <summary>
        /// 
        /// </summary>
        [ConfigurationKeyName("signInViaName")]
        public string SignInViaName
        {
            get;
            set;
        } = SIGNIN_VIA;
        /// <summary>
        /// 
        /// </summary>
        [ConfigurationKeyName("authUri")]
        public string AuthorizationUri
        {
            get;
            set;
        } = AUTH_URI;
        /// <summary>
        /// passport令牌过期时长(秒),默认值7200，从认证成功后，颁发令牌开始计时
        /// </summary>
        [ConfigurationKeyName("passportExpires")]
        public int PassportExpires
        {
            get;
            set;
        } = PASSPORT_EXPIRES;
        /// <summary>
        /// 账户激活票据过期时长(秒),默认值7200
        /// </summary>
        [ConfigurationKeyName("activateExpires")]
        public int ActivateExpires
        {
            get;
            set;
        } = ACTIVATE_EXPIRES;
        /// <summary>
        /// cookie状态过期时长(秒),默认值36000秒(10小时)
        /// </summary>
        [ConfigurationKeyName("cookieStateExpires")]
        public int CookieStateExpires
        {
            get;
            set;
        } = COOKIE_STATE_EXPIRES;
        /// <summary>
        /// 
        /// </summary>
        [ConfigurationKeyName("loginNamePattern")]
        public string LoginNamePattern
        {
            get;
            set;
        } = GlobalKey.REGEX_SECURE_LOGINNAME;
        /// <summary>
        /// 
        /// </summary>
        public Regex LoginNameRegex
        {
            get
            {
                if (_loginNameRegex == null)
                {
                    string pattern = this.LoginNamePattern ?? GlobalKey.REGEX_SECURE_LOGINNAME;
                    _loginNameRegex = new Regex(pattern);
                }
                return _loginNameRegex;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        [ConfigurationKeyName("emailPattern")]
        public string EmailPattern
        {
            get;
            set;
        } = GlobalKey.REGEX_SECURE_EMAILADDRESS;
        /// <summary>
        /// 
        /// </summary>
        public Regex EmailRegex
        {
            get
            {
                if (_emailRegex == null)
                {
                    string pattern = this.EmailPattern ?? GlobalKey.REGEX_SECURE_EMAILADDRESS;
                    _emailRegex = new Regex(pattern);
                }
                return _emailRegex;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        [ConfigurationKeyName("cellphonePattern")]
        public string CellphonePattern
        {
            get;
            set;
        } = GlobalKey.REGEX_SECURE_CELLPHONE;
        /// <summary>
        /// 
        /// </summary>
        public Regex CellphoneRegex
        {
            get
            {
                if (_cellphoneRegex == null)
                {
                    string pattern = this.CellphonePattern ?? GlobalKey.REGEX_SECURE_CELLPHONE;
                    _cellphoneRegex = new Regex(pattern);
                }
                return _cellphoneRegex;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        [ConfigurationKeyName("passwordPattern")]
        public string PasswordPattern
        {
            get;
            set;
        } = GlobalKey.REGEX_SECURE_PASSWORD;
        /// <summary>
        /// 
        /// </summary>
        public Regex PasswordRegex
        {
            get
            {
                if (_passwordRegex == null)
                {
                    string pattern = this.PasswordPattern ?? GlobalKey.REGEX_SECURE_PASSWORD;
                    _passwordRegex = new Regex(pattern);
                }
                return _passwordRegex;
            }
        }

        /// <summary>
        /// 是否禁用同一帐户多源登录(默认值:false)
        /// </summary>
        [ConfigurationKeyName("disableMultiSignIn")]
        public bool DisableMultiSignIn
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        [ConfigurationKeyName("enableRegister")]
        public bool EnableRegister
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        [ConfigurationKeyName("enableScan")]
        public bool EnableScan
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        [ConfigurationKeyName("enableAutoRegister")]
        public bool AutoRegister
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        [ConfigurationKeyName("enableLoginNameSignin")]
        public bool EnableLoginNameSignin
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        [ConfigurationKeyName("enableCellphoneSignin")]
        public bool EnableCellphoneSignin
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        [ConfigurationKeyName("enableRegEmailSignin")]
        public bool EnableRegEmailSignin
        {
            get;
            set;
        }
    }
}
